<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Artifact</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Fira+Mono&display=swap"
        rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <style>
        html,
        body {
            height: 100%;
            margin: 0;
            padding: 0;
            background: #f6f7f9;
            font-family: 'Inter', 'Poppins', 'Helvetica Neue', Arial, sans-serif;
            color: #23272f;
            font-size: 14px;
        }
        .main-container {
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: stretch;
            min-height: 100vh;
            width: 100%;
            background: #f6f7f9;
            gap: 24px;
            padding: 32px 0;
            box-sizing: border-box;
        }
        .chat-box,
        .artifact-pane {
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(40, 60, 90, 0.05);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        .chat-box {
            width: 380px;
            min-width: 300px;
            max-width: 450px;
            margin: 0;
            height: auto;
            min-height: 550px;
            max-height: 88vh;
            flex: 1 1 340px;
        }
        .chat-header {
            font-size: 1.1rem;
            font-weight: 600;
            text-align: left;
            padding: 18px 24px 14px 24px;
            border-bottom: 1px solid #f0f0f4;
            letter-spacing: 0.3px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        #new-chat-btn {
            font-size: 0.85rem;
            font-weight: 500;
            background: #f0f4ff;
            color: #2563eb;
            border: none;
            border-radius: 8px;
            padding: 6px 12px;
            cursor: pointer;
            transition: background 0.2s;
            display: flex;
            align-items: center;
            gap: 4px;
        }
        #new-chat-btn:hover {
            background: #e0e7ff;
        }
        #new-chat-btn .material-icons {
            font-size: 16px;
        }
        .chat-log {
            flex: 1;
            overflow-y: auto;
            padding: 20px 24px 14px 24px;
            display: flex;
            flex-direction: column;
            gap: 14px;
            background: none;
        }
        .message {
            max-width: 92%;
            padding: 0.7em 1em;
            border-radius: 10px;
            font-size: 0.95rem;
            line-height: 1.6;
            word-break: break-word;
            margin: 0.1em 0;
            border: none;
            box-shadow: none;
            position: relative;
        }
        .message.user {
            align-self: flex-end;
            background: #e8f0fe;
            color: #2563eb;
            border-bottom-right-radius: 16px;
            border-top-right-radius: 16px;
            border-top-left-radius: 10px;
            border-bottom-left-radius: 10px;
        }
        .message.bot {
            align-self: flex-start;
            background: #f5f6fa;
            color: #23272f;
            border-bottom-left-radius: 16px;
            border-top-left-radius: 16px;
            border-top-right-radius: 10px;
            border-bottom-right-radius: 10px;
        }
        .code-placeholder {
            margin: 0.6em 0 0.2em 0;
            background: #264653;
            border-radius: 8px;
            min-height: 38px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #e9c46a;
            font-size: 0.9rem;
            font-weight: 500;
            font-family: monospace;
            padding: 12px;
        }
        .code-placeholder-loading {
            animation: pulseBackground 2s infinite ease-in-out;
        }
        .code-placeholder-completed {
            background: #2a6f4b;
            color: #8eed9f;
        }
        .code-placeholder .material-icons {
            margin-right: 8px;
            font-size: 18px;
        }
        .code-placeholder .material-icons.spinning {
            animation: spin 1s infinite linear;
        }
        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }
            25% {
                transform: rotate(90deg);
            }
            50% {
                transform: rotate(180deg);
            }
            75% {
                transform: rotate(270deg);
            }
            100% {
                transform: rotate(360deg);
            }
        }
        @keyframes pulseBackground {
            0% {
                background-color: #264653;
            }
            50% {
                background-color: #1f3a44;
            }
            100% {
                background-color: #264653;
            }
        }
        @keyframes codeLoading {
            0% {
                opacity: 0.5;
            }
            50% {
                opacity: 1;
            }
            100% {
                opacity: 0.5;
            }
        }
        .chat-input-row {
            display: flex;
            align-items: center;
            padding: 16px 24px 20px 24px;
            border-top: 1px solid #f0f0f4;
            background: #fff;
            gap: 10px;
            border-bottom-left-radius: 16px;
            border-bottom-right-radius: 16px;
        }
        #user-input {
            flex: 1;
            padding: 0.8em 1em;
            border-radius: 12px;
            border: 1.5px solid #e5e7eb;
            font-size: 0.95rem;
            outline: none;
            background: #fff;
            margin-right: 0;
            transition: border 0.2s;
        }
        #user-input:focus {
            border-color: #2563eb;
        }
        #send-btn {
            padding: 0.8em 1.8em;
            border-radius: 12px;
            border: none;
            background: #2563eb;
            color: #fff;
            font-size: 0.95rem;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.2s;
            box-shadow: none;
        }
        #send-btn:hover {
            background: #1749b1;
        }
        .artifact-pane {
            width: 500px;
            min-width: 300px;
            max-width: 580px;
            margin: 0;
            height: auto;
            min-height: 550px;
            max-height: 88vh;
            display: none;
            flex-direction: column;
            flex: 1 1 400px;
        }
        .artifact-pane.active {
            display: flex;
        }
        .artifact-tabs {
            display: flex;
            gap: 0;
            padding: 0 24px;
            border-bottom: 1.5px solid #f0f0f4;
            margin-top: 18px;
        }
        .artifact-tab {
            padding: 0.6em 1.4em 0.5em 1.4em;
            border-radius: 10px 10px 0 0;
            background: none;
            color: #23272f;
            cursor: pointer;
            font-weight: 500;
            font-size: 0.95rem;
            border: none;
            margin-bottom: -2px;
            border-bottom: 2.5px solid transparent;
            transition: color 0.18s, border-bottom 0.18s;
        }
        .artifact-tab.active {
            color: #2563eb;
            border-bottom: 2.5px solid #2563eb;
            background: none;
            font-weight: 600;
        }
        .artifact-code,
        .artifact-preview {
            flex: 1;
            overflow: auto;
            background: #23272f;
            border-radius: 0 0 14px 14px;
            padding: 20px 24px;
            margin: 0 24px 24px 24px;
            font-size: 0.95rem;
            min-height: 240px;
            box-sizing: border-box;
        }
        .artifact-code {
            font-family: 'Fira Mono', 'Consolas', monospace;
            background: #23272f;
            color: #fff;
            white-space: pre-wrap;
            font-size: 0.95rem;
        }
        .artifact-preview {
            background: #f8f9fb;
            color: #23272f;
            border-radius: 0 0 14px 14px;
            font-size: 0.95rem;
        }
        .artifact-loading {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #2563eb;
            font-size: 1rem;
            font-family: monospace;
        }
        .artifact-loading .material-icons {
            margin-right: 8px;
            animation: spin 1s infinite linear;
        }
        /* Markdown样式 */
        .message p {
            margin: 0.5em 0;
        }
        .message h1,
        .message h2,
        .message h3,
        .message h4 {
            margin-top: 0.9em;
            margin-bottom: 0.4em;
            font-weight: 600;
        }
        .message h1 {
            font-size: 1.3em;
        }
        .message h2 {
            font-size: 1.2em;
        }
        .message h3 {
            font-size: 1.1em;
        }
        .message h4 {
            font-size: 1em;
        }
        .message a {
            color: #2563eb;
            text-decoration: none;
        }
        .message a:hover {
            text-decoration: underline;
        }
        .message ul,
        .message ol {
            margin: 0.5em 0;
            padding-left: 1.5em;
        }
        .message li {
            margin: 0.25em 0;
        }
        .message blockquote {
            border-left: 3px solid #d1d5db;
            padding-left: 1em;
            margin-left: 0;
            color: #4b5563;
        }
        .message pre {
            background: #f3f4f6;
            padding: 0.7em 1em;
            border-radius: 6px;
            overflow-x: auto;
            margin: 0.7em 0;
        }
        .message code {
            font-family: 'Fira Mono', monospace;
            background: #f3f4f6;
            padding: 0.1em 0.3em;
            border-radius: 3px;
            font-size: 0.9em;
        }
        .message pre code {
            background: transparent;
            padding: 0;
            border-radius: 0;
        }
        .message table {
            border-collapse: collapse;
            width: 100%;
            margin: 0.7em 0;
        }
        .message th,
        .message td {
            border: 1px solid #d1d5db;
            padding: 0.4em;
            text-align: left;
            font-size: 0.9em;
        }
        .message th {
            background: #f3f4f6;
        }
        @media (max-width: 1100px) {
            .main-container {
                flex-direction: column;
                align-items: center;
                gap: 16px;
                padding: 20px 0;
                height: auto;
            }
            .chat-box,
            .artifact-pane {
                width: 92%;
                margin: 0;
                max-width: 580px;
                max-height: none;
                height: auto;
            }
            .chat-box {
                min-height: 450px;
                max-height: 70vh;
            }
            .artifact-pane {
                min-height: 450px;
                max-height: 70vh;
            }
        }
        @media (max-width: 700px) {
            .main-container {
                padding: 12px 0;
            }
            .chat-box,
            .artifact-pane {
                width: 95%;
                min-width: 0;
                border-radius: 14px;
            }
            .chat-log {
                padding: 16px 18px;
            }
            .chat-input-row {
                padding: 14px 18px 18px 18px;
            }
            .artifact-tabs {
                padding: 0 18px;
            }
            .artifact-code,
            .artifact-preview {
                padding: 16px 18px;
                margin: 0 18px 18px 18px;
            }
            .message {
                max-width: 95%;
            }
        }
        @media (max-height: 700px) {
            .main-container {
                padding: 16px 0;
            }
            .chat-box,
            .artifact-pane {
                min-height: 400px;
                max-height: 92vh;
            }
            .chat-header {
                padding: 14px 24px 12px 24px;
            }
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="chat-box">
            <div class="chat-header">
                <span>Artifacts</span>
                <button id="new-chat-btn">
                    <i class="material-icons">add</i>New chat
                </button>
            </div>
            <div class="chat-log" id="chat-log"></div>
            <div class="chat-input-row">
                <input id="user-input" type="text" placeholder="Input..." autocomplete="off" />
                <button id="send-btn">Send</button>
            </div>
        </div>
        <div class="artifact-pane" id="artifact-pane">
            <div class="artifact-tabs">
                <div class="artifact-tab active" id="tab-code">Code</div>
                <div class="artifact-tab" id="tab-preview">Preview</div>
            </div>
            <div class="artifact-code" id="artifact-code"></div>
            <div class="artifact-preview" id="artifact-preview" style="display:none;"></div>
        </div>
    </div>
    <!-- 引入Marked.js库 -->
    <script src="https://cdn.jsdelivr.net/npm/marked@4.3.0/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/dompurify@3.0.5/dist/purify.min.js"></script>
    <script>
        const chatLog = document.getElementById('chat-log');
        const userInput = document.getElementById('user-input');
        const sendBtn = document.getElementById('send-btn');
        const artifactPane = document.getElementById('artifact-pane');
        const artifactCode = document.getElementById('artifact-code');
        const artifactPreview = document.getElementById('artifact-preview');
        const tabCode = document.getElementById('tab-code');
        const tabPreview = document.getElementById('tab-preview');
        const newChatBtn = document.getElementById('new-chat-btn');
        let conversationId = localStorage.getItem('conversation_id') || '';
        let lastArtifactHtml = '';
        let artifactGenerating = false;
        let artifactCodeBuffer = '';
        let artifactMessagePlaceholder = null;
        
    
        function updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock, isLoading = true) {
            if (isLoading) {
                if (artifactMessagePlaceholder) {
                
                    const beforeContent = botMessageContainer.querySelector('.content-before-code');
                    if (beforeContent) {
                        beforeContent.innerHTML = renderMarkdown(contentBeforeCodeblock);
                    } else {
                    
                        const currentHTML = botMessageContainer.innerHTML;
                    
                        const placeholderDiv = botMessageContainer.querySelector('.code-placeholder');
                        if (placeholderDiv) {
                            botMessageContainer.innerHTML = `<div class="content-before-code">${renderMarkdown(contentBeforeCodeblock)}</div>`;
                            botMessageContainer.appendChild(placeholderDiv);
                        } else {
                        
                            botMessageContainer.innerHTML = `<div class="content-before-code">${renderMarkdown(contentBeforeCodeblock)}</div>` +
                                `<div class='code-placeholder code-placeholder-loading'><i class="material-icons spinning">autorenew</i>CODING...</div>`;
                            artifactMessagePlaceholder = botMessageContainer.querySelector('.code-placeholder');
                        }
                    }
                } else {
                
                    botMessageContainer.innerHTML = `<div class="content-before-code">${renderMarkdown(contentBeforeCodeblock)}</div>` +
                        `<div class='code-placeholder code-placeholder-loading'><i class="material-icons spinning">autorenew</i>CODING...</div>`;
                    artifactMessagePlaceholder = botMessageContainer.querySelector('.code-placeholder');
                }
            } else {
            
            
                const afterContent = botMessageContainer.querySelector('.content-after-code');
                if (afterContent) {
                    afterContent.remove();
                }
                
            
                const beforeContent = botMessageContainer.querySelector('.content-before-code');
                if (beforeContent) {
                    beforeContent.innerHTML = renderMarkdown(contentBeforeCodeblock);
                } else {
                
                    const beforeDiv = document.createElement('div');
                    beforeDiv.className = 'content-before-code';
                    beforeDiv.innerHTML = renderMarkdown(contentBeforeCodeblock);
                    botMessageContainer.prepend(beforeDiv);
                }
                
            
                const placeholder = botMessageContainer.querySelector('.code-placeholder');
                if (placeholder) {
                    placeholder.className = 'code-placeholder code-placeholder-completed';
                    placeholder.innerHTML = '<i class="material-icons">check_circle</i>Code generation completed';
                } else {
                    const completeDiv = document.createElement('div');
                    completeDiv.className = 'code-placeholder code-placeholder-completed';
                    completeDiv.innerHTML = '<i class="material-icons">check_circle</i>Code generation completed';
                    
                
                    const beforeContent = botMessageContainer.querySelector('.content-before-code');
                    if (beforeContent) {
                        beforeContent.after(completeDiv);
                    } else {
                        botMessageContainer.appendChild(completeDiv);
                    }
                }
            }
        }
        
        marked.setOptions({
            gfm: true,
            breaks: true,
            sanitize: false,
            smartypants: true,
            highlight: function (code, lang) {
                return code;
            }
        });
        tabCode.onclick = function () {
            tabCode.classList.add('active');
            tabPreview.classList.remove('active');
            artifactCode.style.display = '';
            artifactPreview.style.display = 'none';
        };
        tabPreview.onclick = function () {
            tabPreview.classList.add('active');
            tabCode.classList.remove('active');
            artifactCode.style.display = 'none';
            artifactPreview.style.display = '';
            
            if (artifactGenerating) {
                artifactPreview.innerHTML = `<div class='artifact-loading'><i class="material-icons">autorenew</i>CODING...</div>`;
            } else if (lastArtifactHtml) {
                try {
                    // 清空预览区域
                    artifactPreview.innerHTML = '';
                    
                    // 创建一个blob URL
                    const blob = new Blob([lastArtifactHtml], {type: 'text/html'});
                    const blobUrl = URL.createObjectURL(blob);
                    
                    // 创建iframe并设置src为blob URL
                    const iframe = document.createElement('iframe');
                    iframe.style.width = '100%';
                    iframe.style.height = '100%';
                    iframe.style.border = 'none';
                    iframe.src = blobUrl;
                    
                    // 添加iframe到预览区域
                    artifactPreview.appendChild(iframe);
                    
                    // 在iframe加载完成后释放blob URL
                    iframe.onload = function() {
                        URL.revokeObjectURL(blobUrl);
                    };
                } catch (error) {
                    artifactPreview.innerHTML = '无法预览内容: ' + error.message;
                }
            } else {
                artifactPreview.innerHTML = '';
            }
        };
        function appendMessage(text, sender) {
            const msg = document.createElement('div');
            msg.className = 'message ' + sender;
            msg.innerHTML = renderMarkdown(text);
            chatLog.appendChild(msg);
            chatLog.scrollTop = chatLog.scrollHeight;
            return msg;
        }
        function showArtifact(htmlCode) {
            artifactPane.classList.add('active');
            artifactCode.textContent = htmlCode;
            if (!tabCode.classList.contains('active')) tabCode.classList.add('active');
            tabPreview.classList.remove('active');
            artifactCode.style.display = '';
            artifactPreview.style.display = 'none';
            lastArtifactHtml = htmlCode;
        }
        function hideArtifact() {
            artifactPane.classList.remove('active');
            artifactCode.textContent = '';
            artifactPreview.innerHTML = '';
            lastArtifactHtml = '';
            artifactGenerating = false;
            artifactCodeBuffer = '';
            artifactMessagePlaceholder = null;
        }
        async function sendMessage() {
            const message = userInput.value.trim();
            if (!message) return;
            appendMessage(message, 'user');
            userInput.value = '';
            const requestBody = {
                query: message,
                conversation_id: conversationId
            };
            try {
                const response = await fetch(window.location.pathname + 'api/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestBody)
                });
                if (!response.ok) throw new Error('Network response was not ok');
                let botMessageContainer = appendMessage('', 'bot');
                const reader = response.body.getReader();
                const decoder = new TextDecoder('utf-8');
                let buffer = '';
                let accumulatedText = '';
                let codeBlockStarted = false;
                let codeBlockEnded = false;
                artifactGenerating = false;
                artifactCodeBuffer = '';
                artifactMessagePlaceholder = null;
                const htmlBlockStartRegex = /```html[\r\n]+/;
                const htmlBlockCompleteRegex = /```html[\r\n]+([\s\S]*?)```/;
                while (true) {
                    const { value, done } = await reader.read();
                    if (done) break;
                    buffer += decoder.decode(value, { stream: true });
                    const lines = buffer.split('\n\n');
                    buffer = lines.pop() || '';
                    for (const line of lines) {
                        if (!line.trim()) continue;
                        try {
                            const data = JSON.parse(line);
                            if (data.answer) {
                                accumulatedText += data.answer;
                                if (!codeBlockStarted) {
                                    const codeStartMatch = accumulatedText.match(htmlBlockStartRegex);
                                    if (codeStartMatch) {
                                        codeBlockStarted = true;
                                        artifactGenerating = true;
                                        artifactCodeBuffer = '';
                                        const codeStartIndex = accumulatedText.indexOf('```html');
                                        const contentBeforeCodeblock = accumulatedText.substring(0, codeStartIndex);
                                        updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock);
                                        artifactPane.classList.add('active');
                                        tabCode.classList.add('active');
                                        tabPreview.classList.remove('active');
                                        artifactCode.style.display = '';
                                        artifactPreview.style.display = 'none';
                                        artifactCode.textContent = 'CODING...';
                                        artifactPreview.innerHTML = '';
                                        artifactMessagePlaceholder = botMessageContainer.querySelector('.code-placeholder');
                                    }
                                }
                                if (codeBlockStarted && !codeBlockEnded) {
                                    const codeMatchComplete = accumulatedText.match(htmlBlockCompleteRegex);
                                    if (codeMatchComplete) {
                                        artifactCodeBuffer = codeMatchComplete[1];
                                        codeBlockEnded = true;
                                        artifactGenerating = false;
                                        const codeStartIndex = accumulatedText.indexOf('```html');
                                        const codeEndIndex = accumulatedText.indexOf('```', codeStartIndex + 6) + 3;
                                        const contentBeforeCodeblock = accumulatedText.substring(0, codeStartIndex);
                                        let contentAfterCodeblock = accumulatedText.substring(codeEndIndex);
                                        
                                        contentAfterCodeblock = contentAfterCodeblock.trim();
                                        
                                        updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock, false);
                                        if (contentAfterCodeblock) {
                                            const afterContent = document.createElement('div');
                                            afterContent.className = 'content-after-code';
                                            afterContent.innerHTML = renderMarkdown(contentAfterCodeblock);
                                            botMessageContainer.appendChild(afterContent);
                                        }
                                        
                                        artifactCode.textContent = artifactCodeBuffer;
                                        lastArtifactHtml = artifactCodeBuffer;
                                        
                                        // 预加载iframe以便立即渲染
                                        if (tabPreview.classList.contains('active')) {
                                            tabPreview.onclick();
                                        }
                                    } else {
                                        const codeStartIndex = accumulatedText.indexOf('```html');
                                        if (codeStartIndex !== -1) {
                                            const partialCode = accumulatedText.substring(codeStartIndex + 7);
                                            artifactCodeBuffer = partialCode.replace(/^\s*\n/, '');
                                            artifactCode.textContent = artifactCodeBuffer;
                                            const contentBeforeCodeblock = accumulatedText.substring(0, codeStartIndex);
                                            updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock);
                                        }
                                    }
                                } else if (!codeBlockStarted || codeBlockEnded) {
                                
                                    if (codeBlockEnded) {
                                    
                                        const codeStartIndex = accumulatedText.indexOf('```html');
                                        const codeEndIndex = accumulatedText.indexOf('```', codeStartIndex + 6) + 3;
                                        const contentBeforeCodeblock = accumulatedText.substring(0, codeStartIndex);
                                        let contentAfterCodeblock = accumulatedText.substring(codeEndIndex);
                                        
                                        contentAfterCodeblock = contentAfterCodeblock.trim();
                                        
                                        updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock, false);
                                        if (contentAfterCodeblock) {
                                            const afterContent = document.createElement('div');
                                            afterContent.className = 'content-after-code';
                                            afterContent.innerHTML = renderMarkdown(contentAfterCodeblock);
                                            botMessageContainer.appendChild(afterContent);
                                        }
                                    } else {
                                    
                                        botMessageContainer.innerHTML = renderMarkdown(accumulatedText);
                                    }
                                }
                            }
                            if (data.conversation_id) {
                                conversationId = data.conversation_id;
                                localStorage.setItem('conversation_id', conversationId);
                            }
                        } catch (error) {
                        }
                    }
                }
                if (codeBlockEnded && lastArtifactHtml) {
                
                    tabPreview.onclick();
                    
                
                    const codeStartIndex = accumulatedText.indexOf('```html');
                    const codeEndIndex = accumulatedText.indexOf('```', codeStartIndex + 6) + 3;
                    const contentBeforeCodeblock = accumulatedText.substring(0, codeStartIndex);
                    let contentAfterCodeblock = accumulatedText.substring(codeEndIndex);
                    
                    contentAfterCodeblock = contentAfterCodeblock.trim();
                    
                    updateCodePlaceholder(botMessageContainer, contentBeforeCodeblock, false);
                    if (contentAfterCodeblock) {
                        const afterContent = document.createElement('div');
                        afterContent.className = 'content-after-code';
                        afterContent.innerHTML = renderMarkdown(contentAfterCodeblock);
                        botMessageContainer.appendChild(afterContent);
                    }
                }
                if (!codeBlockStarted) {
                    hideArtifact();
                }
            } catch (error) {
                appendMessage('Error...', 'bot');
                hideArtifact();
            }
        }
        function startNewConversation() {
        
            chatLog.innerHTML = '';
            
        
            conversationId = '';
            localStorage.removeItem('conversation_id');
            
        
            hideArtifact();
            globalAccumulatedText = '';
            globalCodeBlockStarted = false;
            globalCodeBlockEnded = false;
            
        
            appendMessage('New Chat', 'bot');
        }
        
    
        newChatBtn.addEventListener('click', startNewConversation);
        
    
        sendBtn.addEventListener('click', () => sendMessage());
        userInput.addEventListener('keypress', function (event) {
            if (event.key === 'Enter') sendMessage();
        });
        function renderMarkdown(text, codeBlockStarted, codeBlockEnded) {
            let renderedHtml = marked.parse(text);
            renderedHtml = DOMPurify.sanitize(renderedHtml);
            return renderedHtml;
        }
        function escapeHtml(str) {
            return str.replace(/[&<>]/g, function (tag) {
                const chars = { '&': '&amp;', '<': '&lt;', '>': '&gt;' };
                return chars[tag] || tag;
            });
        }
    </script>
</body>
</html>